package controllers;

import helpers.ConsoleUtils;

import java.util.Date;
import java.util.List;

import org.codehaus.jackson.node.ObjectNode;

import be.objectify.deadbolt.java.actions.Group;
import be.objectify.deadbolt.java.actions.Restrict;

import models.Resource;
import models.ResourceType;
import play.data.Form;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import services.ConfigurationService;
import views.html.configs.*;

public class Configurations extends Controller {

	private static final ConfigurationService configurationService = ConfigurationService.getService();

	@Restrict(@Group(Application.ROLE_USER))
	public static Result listConfigs() {
		List<Resource> configs = configurationService.getAllConfigs();
		return ok(list.render(configs));
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result addConfig() {
		Form<Resource> configForm = new Form<Resource>(Resource.class);
		return ok(add.render(configForm));
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result editConfig(long configId) {
		Resource config = configurationService.getConfig(configId);
		if (config != null) {
			Form<Resource> form = Form.form(Resource.class).fill(config);
			String stringConfig = configurationService.getConfigFileContent(config);
			form.data().put("configuration-content", stringConfig);
			return ok(edit.render(config, form));
		} else {
			return TODO;
		}
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result createConfig() {
		Form<Resource> form = Form.form(Resource.class).bindFromRequest();
		if (!form.hasErrors()) {
			Resource config = form.get();
			String configContent = Form.form().bindFromRequest().get("configuration-content");
			config.setCreatedDate(new Date());
			config.setType(ResourceType.TestConfiguration);
			config.setLastModifiedDate(config.getCreatedDate());
			config.setCompany(ConsoleUtils.getCompany());
			configurationService.saveConfig(config.getCompany(), config, configContent);
			return redirect(controllers.routes.Configurations.listConfigs());
		} else {
			return ok(add.render(form));
		}
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result updateConfig(long configId) {
		Resource config = configurationService.getConfig(configId);
		Form<Resource> form = Form.form(Resource.class).bindFromRequest();
		if (!form.hasErrors()) {
			Resource newConfig = form.get();
			config.setName(newConfig.getName());
			config.setDescription(newConfig.getDescription());
			config.setLastModifiedDate(new Date());

			String stringConfig = Form.form().bindFromRequest().get("configuration-content");

			if (config.getProject() != null) {
				configurationService.saveConfig(config.getProject(), config, stringConfig);
			} else {
				configurationService.saveConfig(config.getCompany(), config, stringConfig);
			}
			return redirect(controllers.routes.Configurations.editConfig(configId));
		} else {
			return ok(edit.render(config, form));
		}
	}

	@Restrict(@Group(Application.ROLE_USER))
	public static Result getConfigContentRaw(long configId) {
		ObjectNode result = Json.newObject();
		if (configurationService.configExists(configId)) {
			Resource config = configurationService.getConfig(configId);
			result.put("content", configurationService.getConfigFileContent(config));
			return ok(result);
		} else {
			result.put("content", "");
			return notFound(result);
		}
	}

}
